package com.mall.security.common.manager;

import com.mall.common.exceptions.ShopException;
import com.mall.common.utils.IpHelper;
import com.mall.common.utils.RedisEnums;
import com.mall.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Component
public class PasswordManager {

    private static final int TIMES_CHECK_INPUT_PASSWORD_NUM = 10;

    public void checkLoginTime(String username,String loginPassword,String Password){
        String prefix= RedisEnums.CHECK_PWD_TIME+ username + IpHelper.getIpAddr();
        int count = 0;
        //判断是否有值
        if(!RedisUtils.hasKey(prefix)){
            //得到count值
            count = RedisUtils.get(prefix);
        }
        if(count >= TIMES_CHECK_INPUT_PASSWORD_NUM){
            throw new ShopException("已经输入错误10次，请30分钟后再尝试");
        }
        if(loginPassword==null || !Password.equals(loginPassword)){
            count++;
            RedisUtils.set(prefix,count,1800,TimeUnit.SECONDS);
            throw new ShopException("密码错误，请重新输入");
        }
        //密码正确 放行
        RedisUtils.del(prefix);


        //判断是否有值
        //得到count值
        //判断count值是否>0

        //count=10设置过期30分钟，
     /*   if(RedisUtils.hasKey(prefix)){
            count = RedisUtils.get(prefix);
            if(count<=10){
                //累加
                RedisUtils.inc(prefix,1);
                RedisUtils.expire(prefix,3600);
            }else {
                throw new ShopException("已经输入错误10次，请30分钟后再尝试");
            }
        }else {

            RedisUtils.set(prefix,1,3600, TimeUnit.SECONDS);
        }
        */

    }
}
